﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Poligono.Core;
using System.Data.Common;
using Poligono.Core.Exceptions;

namespace Poligono.Data
{
    /// <summary>
    /// Maker a wrapper to type command
    /// <remarks>
    /// the type difine like as the BdUtil will use in DataBase
    /// </remarks>
    /// </summary>
    internal class TypeCommandWrapper : DisposeControl
    {
        private const int _DEFAULT_COMMAND_TIMEOUT = 30;

        int _commandTimeOut = _DEFAULT_COMMAND_TIMEOUT;
        //CommandType commandType;
        //string commandText;
        DbCommand dbCommand; //Never need create a Command, he will get DbCommando of Scope

        private DbCommand DbCommand
        {
            get
            {
                CheckDispose();
                return dbCommand;
            }
            set
            {
                CheckDispose();
                dbCommand = value;
            }
        }

        public int CommandTimeOut
        {
            get
            {
                if (dbCommand != null)
                    return dbCommand.CommandTimeout;
                else
                    throw new PoligonoException();
            }
            set
            {
                if (dbCommand != null)
                    dbCommand.CommandTimeout = _commandTimeOut;
                else
                    throw new PoligonoException();
                //_commandTimeOut = value;
            }
        }

        /// <summary>
        /// Get/Set CommandType of <see cref="DbCommand"/>
        /// </summary>
        public CommandType CommandType
        {
            get
            {
                if (DbCommand == null)
                    throw new PoligonoException();
                return DbCommand.CommandType;
            }
            set
            {
                if (DbCommand == null)
                    throw new PoligonoException();
                DbCommand.CommandType = value;
            }
        }

        /// <summary>
        /// Get/Set CommandText of <see cref="DbCommand"/>
        /// </summary>
        public string CommandText
        {
            get
            {
                if (DbCommand == null)
                    throw new PoligonoException();
                return DbCommand.CommandText;
            }
            set
            {
                if (DbCommand == null)
                    throw new PoligonoException();
                DbCommand.CommandText = value;
            }
        }

        /// <summary>
        /// Menssager of erro <see cref="ObjectDisposedException"/>
        /// </summary>
        protected override string MensagemErroObjectDisposedException
        {
            get { return "TypeCommandWrapper"; }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataAcessProvider"></param>
        /// <returns></returns>
        internal DbCommand GetDbCommand(DataAcessProvider dataAcessProvider)
        {
            CheckDispose();
            if (dbCommand == null)
            {
                DbCommand = DbUtil.NewDbCommand(dataAcessProvider, CommandText, CommandType, CommandTimeOut);

            }
            return dbCommand;
        }
    }
}
